﻿using System;
using System.Collections.Generic;
using System.Web;
using Cashflow.DAL;

namespace Cashflow.BLL.ReEx
{
	public class ReEx : BizObject
	{
		private long _id = 0;
		public long ID { get { return _id; } set { _id = value; } }

		private DateTime _date = DateTime.Now;
		public DateTime Date { get { return _date; } set { _date = value; } }

		private decimal _amount = decimal.MinValue;
		public decimal Amount { get { return _amount; } set { _amount = value; } }

		private string _note = "";
		public string Note { get { return _note; } set { _note = value; } }

		public ReEx(long id, DateTime date, decimal amount, string note)
		{
			_id = id;
			_date = date;
			_amount = amount;
			_note = note;
		}

		public bool Update()
		{
			return ReEx.UpdateReEx(this.ID, this.Date, this.Amount, this.Note);
		}

		public bool Delete()
		{
			bool ret = ReEx.DeleteReEx(this.ID);
			if(ret) this.ID = 0;
			return ret;
		}

		public static ReEx GetReExByID(long id)
		{
			ReEx item = null;
			string key = "ReEx_id" + id.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				item = (ReEx)Cache[key];
			else
			{
				item = GetReExFromDetails(SiteProvider.ReEx.GetReExByID(id));
				CacheData(key, item);
			}
			return item;
		}

		public static List<ReEx> GetReExsFromTo(DateTime from, DateTime to)
		{
			List<ReEx> items = new List<ReEx>();
			string key = "ReExs_from" + from.ToString() + "_to" + to.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				items = (List<ReEx>)Cache[key];
			else
			{
				items = GetReExListFromDetailsList(SiteProvider.ReEx.GetReExsFromTo(from, to));
				CacheData(key, items);
			}
			return items;
		}

		public static List<ReEx> GetReExsByYear(int year)
		{
			List<ReEx> items = new List<ReEx>();
			string key = "ReExs_year" + year.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				items = (List<ReEx>)Cache[key];
			else
			{
				items = GetReExListFromDetailsList(SiteProvider.ReEx.GetReExsByYear(year));
				CacheData(key, items);
			}
			return items;
		}

		public static List<ReEx> GetReExsByMonth(int year, int month)
		{
			List<ReEx> items = new List<ReEx>();
			string key = "ReExs_year" + year.ToString() + "_month" + month.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				items = (List<ReEx>)Cache[key];
			else
			{
				items = GetReExListFromDetailsList(SiteProvider.ReEx.GetReExsByMonth(year, month));
				CacheData(key, items);
			}
			return items;
		}

		public static List<ReEx> GetReExsByDay(int year, int month, int day)
		{
			List<ReEx> items = new List<ReEx>();
			string key = "ReExs_year" + month.ToString() + "_month" + month.ToString() + "_day" + day.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				items = (List<ReEx>)Cache[key];
			else
			{
				items = GetReExListFromDetailsList(SiteProvider.ReEx.GetReExsByDay(year, month, day));
				CacheData(key, items);
			}
			return items;
		}

		public static long InsertReEx(DateTime date, decimal amount, string note)
		{
			note = ConvertNullToEmptyString(note);

			ReExDetails item = new ReExDetails(0, date, amount, note);
			long ret = SiteProvider.ReEx.InsertReEx(item);
			PurgeCacheItems("ReEx");

			return ret;
		}

		public static bool UpdateReEx(long id, DateTime date, decimal amount, string note)
		{
			note = ConvertNullToEmptyString(note);

			ReExDetails item = new ReExDetails(id, date, amount, note);
			bool ret = SiteProvider.ReEx.UpdateReEx(item);
			PurgeCacheItems("ReEx");

			return ret;
		}

		public static bool DeleteReEx(long id)
		{
			bool ret = SiteProvider.ReEx.DeleteReEx(id);
			PurgeCacheItems("ReEx");

			return ret;
		}

		private static ReEx GetReExFromDetails(ReExDetails record)
		{
			if (record == null) return null;
			else
				return new ReEx(record.ID, record.Date, record.Amount, record.Note);
		}

		private static List<ReEx> GetReExListFromDetailsList(List<ReExDetails> recordset)
		{
			List<ReEx> items = new List<ReEx>();
			foreach (ReExDetails record in recordset)
				items.Add(GetReExFromDetails(record));
			return items;
		}
	}
}